const TerserPlugin = require('terser-webpack-plugin')
const path = require('path')

module.exports = {
  runtimeCompiler: true,
  publicPath: process.env.NODE_ENV === 'development' ? '/' : '/luan/',
  // publicPath: process.env.NODE_ENV === 'production' ? SYSTEM_CONFIG.publicPath : '/',
  productionSourceMap: false,
  devServer: {
    port: 3110,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    }
    // before: require('./src/mock/index')
  },
  configureWebpack: {
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
        '@@': path.resolve(__dirname, 'src/assets')
      }
    },
    externals: {
      AMap: 'window.AMap',
      appID: 'window.appID',
      // jQuery: 'window.jQuery'
    },
    plugins: [
    ],
    optimization: {
      minimize: process.env.NODE_ENV != 'development',
      minimizer: [
        new TerserPlugin({
          terserOptions: {
            compress: {
              drop_console: process.env.NODE_ENV != 'development',
              drop_debugger: process.env.NODE_ENV != 'development'
            }
          }
        })
      ],
    },
    devtool: 'none'
  },
  // variables全局引入
  // 1. vue add style-resources-loader 2. 配置vue.config.js
  pluginOptions: {
    'style-resources-loader': {
      preProcessor: 'scss',
      patterns: [
        path.resolve(__dirname, 'src/assets/style/_variables.scss'),
        path.resolve(__dirname, 'src/assets/style/global.scss')
      ]
    }
  },
  // chainWebpack
  chainWebpack: config => {
    config.module
      .rule('svg')
      .exclude.add(path.resolve('src/assets/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(path.resolve('src/assets/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    // 删除换行而加的配置
    config.module
      .rule('vue')
      .use('vue-loader')
      .loader('vue-loader')
      .tap(options => {
        options.compilerOptions.preserveWhitespace = true
        return options
      })
      .end()
    // // 开启器 Gzip 压缩
    // if (process.env.NODE_ENV != 'development') {
    //   config.plugin('compressionPlugin')
    //     .use(new CompressionPlugin({
    //       filename: '[path].gz[query]',
    //       algorithm: 'gzip',
    //       test: productionGzipExtensions,
    //       threshold: 10240,
    //       minRatio: 0.8,
    //       deleteOriginalAssets: false
    //     }))
    // }
  },
  transpileDependencies: [
    'vue-echarts',
    'resize-detector'
  ]
}
